﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace YJKArch.AxisModule
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdGridTagShowHide : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			this.m_uiDoc = cmdData.Application.ActiveUIDocument;
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_app = cmdData.Application.Application;
			this.m_selection = cmdData.Application.ActiveUIDocument.Selection;
			RevitVersionFuncs.ClearSelection(this.m_selection);
			if (CmdGridTagShowHide._hWndRevit == null)
			{
				CmdGridTagShowHide._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			this.LoadFamily();
			GridTagShowHideFrm gridTagShowHideFrm = new GridTagShowHideFrm(cmdData);
			gridTagShowHideFrm.Show(CmdGridTagShowHide._hWndRevit);
			AxisFilter axisFilter = new AxisFilter();
			int wMsg = 7;
			CmdGridTagShowHide.SendMessage(CmdGridTagShowHide._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			for (;;)
			{
				IL_AF:
				List<Element> list = null;
				for (;;)
				{
					try
					{
						list = new List<Element>();
						if (gridTagShowHideFrm.SelMode == 1)
						{
							Reference reference = this.m_selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, axisFilter, "请选择轴线:");
							list.Add(this.m_doc.GetElement(reference));
						}
						else
						{
							list = (this.m_uiDoc.Selection.PickElementsByRectangle(axisFilter, "请选择轴线:") as List<Element>);
						}
						if (list.Count == 0)
						{
							goto IL_AF;
						}
					}
					catch (Exception)
					{
						if (gridTagShowHideFrm.m_clickModeBtn)
						{
							gridTagShowHideFrm.m_clickModeBtn = false;
							continue;
						}
						gridTagShowHideFrm.Close();
						return Autodesk.Revit.UI.Result.Succeeded;
					}
					break;
				}
				RequestParam requestParam = new RequestParam
				{
					m_commandData = cmdData,
					m_Frm = gridTagShowHideFrm,
					m_lstGrid = new List<Element>()
				};
				requestParam.m_lstGrid.AddRange(list);
				new HideOrShowGridTag().DoRequest(requestParam);
			}
			Result result;
			return result;
		}

		private void LoadFamily()
		{
			bool flag = false;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(Family)).ToElements();
			IEnumerator<Element> enumerator = filteredElementCollector.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((Family)enumerator.Current).Name.CompareTo("Grid_Head_Null") == 0)
					{
						flag = true;
						break;
					}
				}
			}
			if (!flag)
			{
				string text = AssistFunc.GetAppRootPath();
				string versionNumber = this.m_app.VersionNumber;
				text = text + "\\Templates\\" + versionNumber + "\\Grid_Head_Null.rfa";
				if (!File.Exists(text))
				{
					throw new Exception("Unsupported version");
				}
				Transaction transaction = new Transaction(this.m_doc);
				transaction.Start("load families");
				this.m_doc.LoadFamily(text);
				transaction.Commit();
			}
		}

		public const int HideOrShowRequestId = 10;

		public static WindowHandle _hWndRevit;

		private UIDocument m_uiDoc;

		private Autodesk.Revit.DB.Document m_doc;

		private Application m_app;

		private Selection m_selection;
	}
}
